//
//  TrackReplayView.m
//  student
//
//  Created by JCai on 2018/10/25.
//  Copyright © 2018 higer. All rights reserved.
//

#import "TrackReplayView.h"
#import "TrackPictureView.h"
#import "ExamPlayInfoModel.h"
#import "TrackProgressView.h"

@interface TrackReplayView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) TrackPictureView *pictureView;
@property (nonatomic, assign) CGFloat scale;
@property (nonatomic, strong) NSArray *pathsArray;
@property (nonatomic, assign) NSInteger pathSelected;
@property (nonatomic, assign) CFAbsoluteTime startTime;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) TrackProgressView *progressView;
@property (nonatomic, assign) NSInteger pathAddIndex;
@property (nonatomic, assign) CGFloat sleepTime;


@end

@implementation TrackReplayView

- (void)dealloc
{
    [self stopTimer];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scale = 1.0;
        self.pathAddIndex = 1;
        self.sleepTime = 1.0;
        CGRect scrollFrame = CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
        scrollView.delegate = self;
        [scrollView setContentSize:CGSizeMake(3384, 3000)];
        [scrollView setMinimumZoomScale:0.5f];
        [scrollView setMaximumZoomScale:1.0f];
        [scrollView setZoomScale:0.5f animated:NO];
        [scrollView setUserInteractionEnabled: YES];
        [scrollView setShowsHorizontalScrollIndicator: YES];//是否显示侧边的滚动栏
        [scrollView setShowsVerticalScrollIndicator: NO];
        [scrollView setScrollsToTop: NO];
        [scrollView setScrollEnabled: YES];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        TrackPictureView *pictureView = [[TrackPictureView alloc] initWithFrame:CGRectMake(0, 0, 3384, 3000)];
        [self.scrollView addSubview:pictureView];
        self.pictureView = pictureView;
        
        [self reloadFrame:CGSizeMake(3384, 3000)];
        [self addNotification];
        
        CGRect progressFrame = CGRectMake(0, CGRectGetHeight(frame)-50*SCALING, CGRectGetWidth(frame), 50 *SCALING);
        TrackProgressView *progressView = [[TrackProgressView alloc] initWithFrame:progressFrame];
        [self addSubview:progressView];
        self.progressView = progressView;
        
        WS(ws);
        [self.progressView setRateBlock:^(NSInteger index) {
            ws.pathAddIndex = index;
            ws.sleepTime = 1.0 / (CGFloat)index;
        }];
    }
    return self;
}

#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.pictureView;
}
//当滑动结束时
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    //把当前的缩放比例设进ZoomScale，以便下次缩放时实在现有的比例的基础上
    NSLog(@"scale is %f",scale);
    self.scale = scale;
    [self.scrollView setZoomScale:scale animated:NO];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = self.scrollView.frame.size.height / scale;
    zoomRect.size.width  = self.scrollView.frame.size.width  / scale;
    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
    return zoomRect;
}

#pragma mark -
#pragma mark -
- (void)addNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appDidBecomeActive:)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appWillResignActive:)
                                                 name:UIApplicationWillResignActiveNotification
                                               object:nil];
}

#pragma mark App 从后台进入前台
- (void)appDidBecomeActive:(NSNotification *)notify
{
    CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
    self.startTime = currentTime - self.pathSelected;
    [self startTimer];
}

#pragma mark App 从前台进入后台
- (void)appWillResignActive:(NSNotification *)notify
{
    [self stopTimer];
}

-(void)startTimer
{
    self.timer = [NSTimer timerWithTimeInterval:1
                                         target:self
                                       selector:@selector(clockDidTick:)
                                       userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)clockDidTick:(NSTimer *)timer {
    CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
    self.pathSelected = (int)(currentTime - self.startTime);
    if (self.pathsArray.count <= self.pathSelected) {
        [self stopTimer];
        return ;
    }
    
    [self performSelectorOnMainThread:@selector(reloadLineLayer) withObject:nil waitUntilDone:NO];
}

- (void)stopTimer
{
    DLog(@"111111");
    if (self.timer) {
        DLog(@"111111-111111111");
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void)reloadLineLayer
{
    [self.progressView reloadProgress:self.pathSelected];
    [self.pictureView reloadLineLayer:self.pathSelected];
    
    ExamPlayInfo *playInfo = [self.pathsArray objectAtIndex:self.pathSelected];
    CGSize scrollContentSize = self.scrollView.contentSize;
    CGFloat pox = playInfo.pox * self.scale;
    CGFloat poy = playInfo.poy * self.scale;
    CGFloat viewWidth = self.bounds.size.width;
    CGFloat viewHeight = self.bounds.size.height;
    if (pox <= viewWidth * 0.5) {
        pox = 0;
    }
    else if (pox >= scrollContentSize.width - viewWidth * 0.5) {
        pox = scrollContentSize.width - viewWidth;
    }
    else {
        pox -= viewWidth * 0.5;
    }
    if (poy <= viewHeight * 0.5) {
        poy = 0;
    }
    else if (poy >= scrollContentSize.height - viewHeight * 0.5) {
        poy = scrollContentSize.height - viewHeight;
    }
    else {
        poy -= viewHeight * 0.5;
    }
    CGPoint scrollOffset = CGPointMake(pox, poy);
    [self.scrollView setContentOffset:scrollOffset animated:YES];
}

- (void)reloadFrame:(CGSize)viewSize
{
    [self.scrollView setContentSize:viewSize];
    
    CGRect pictureFrame = self.pictureView.frame;
    pictureFrame.size = viewSize;
    [self.pictureView setFrame:pictureFrame];
    
    [self.pictureView  reloadPicture:viewSize];
}

#pragma mark ==========车辆轨迹加载==========
- (void)reloadDrawing:(NSArray *)paths :(NSInteger)videoLength
{
    self.pathsArray = paths;
    self.pathSelected = 0;
    
    [self.progressView setEndTime:videoLength];
    [self.progressView reloadProgress:self.pathSelected];
    
    CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
    self.startTime = currentTime - self.pathSelected;
    [self.pictureView reloadDrawing:paths];
    [self startTimer];
}


@end
